package com.hopoke.user.controller;

import com.hopoke.common.domain.Result;
import com.hopoke.common.util.UserContext;
import com.hopoke.model.domain.dto.user.LoginDTO;
import com.hopoke.model.domain.dto.user.UserDTO;
import com.hopoke.model.domain.po.user.User;
import com.hopoke.model.domain.vo.user.LoginVO;
import com.hopoke.model.domain.vo.user.ValidateCodeVO;
import com.hopoke.user.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@RestController
@CrossOrigin
@RequestMapping("/user")
@RequiredArgsConstructor
@Api(tags = "用户相关接口")
public class UserController {

    private final UserService userService;

    @ApiOperation("根据token获取用户接口")
    @GetMapping("/getUserInfo")
    public Result<User> getUserInfo() {
        return Result.ok(UserContext.getUser());
    }

    @ApiOperation("用户退出登录接口")
    @GetMapping("/logout")
    public Result<String> logout(@RequestHeader String token){
        userService.logout(token);
        return Result.ok();
    }

    public Result<User> getUserById(@PathVariable Long id){
        User user = userService.getById(id);
        return Result.ok(user);
    }

    @ApiOperation("登录接口")
    @PostMapping("/login")
    public Result<LoginVO> login(@RequestBody LoginDTO loginDTO){
        LoginVO loginVO = userService.login(loginDTO);
        return Result.ok(loginVO);
    }

    @ApiOperation("生成验证码")
    @GetMapping("/generateValidateCode")
    public Result<ValidateCodeVO> generateValidateCode(){
        ValidateCodeVO validateCodeVO = userService.generateValidateCode();
        return Result.ok(validateCodeVO);
    }

    @ApiOperation("修改用户信息")
    @PostMapping("/update")
    public Result<String> update(@RequestBody UserDTO userDTO){

        User user = userService.getById(userDTO.getUId());
//        user.setRoleId(userDTO.getRoleId());
        boolean update = userService.updateById(user);
        return Result.ok("修改成功");
    }

}
